//吴亚举无线串口自己玩 可以无线printf 无线收发
#include "AS1217_usart.h"

common_usart usart2={USART2,GPIOA,GPIOA,GPIO_Pin_2,GPIO_Pin_3, 9600,0};


void drv_uart_init(void)
{
	common_usart usart=usart2;
	//GPIO端口设置
	GPIO_InitTypeDef  GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	NVIC_InitTypeDef  NVIC_InitStructure;

  //使能USART1，GPIOA时钟 !!!!!!
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);

	//USART1_TX   
	GPIO_InitStructure.GPIO_Pin = usart.GPIOTx_Pin;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;	//复用推挽输出
	GPIO_Init(usart.GPIOTx, &GPIO_InitStructure);

	//USART1_RX	  
	GPIO_InitStructure.GPIO_Pin = usart.GPIORx_Pin;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
	GPIO_Init(usart.GPIORx, &GPIO_InitStructure); 

	//Usart1 NVIC 配置!!!!!
	NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2 ;//抢占优先级3
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;		//子优先级3
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;			//IRQ通道使能
	NVIC_Init(&NVIC_InitStructure);	//根据指定的参数初始化VIC寄存器

	//USART 初始化设置
	USART_InitStructure.USART_BaudRate = usart.bound;//串口波特率
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
	USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
	USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;	//收发模式

	USART_Init(USART2, &USART_InitStructure); //初始化串口1
	USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//开启串口接受中断
	USART_Cmd(USART2, ENABLE);                    //使能串口1 
}

/**
  * @brief :串口发送数据
  */
void drv_uart_tx_bytes( uint8_t* TxBuffer, uint8_t Length )
{
	while( Length-- )
	{
		while( RESET == USART_GetFlagStatus(usart2.Usart, USART_FLAG_TXE ));
		usart2.Usart->DR = *TxBuffer;
		TxBuffer++;
	}
}

/**
  * @brief :串口接收数据
  */

void USART2_IRQHandler(void)                	
	{
	u8 Res;
#if SYSTEM_SUPPORT_OS 		//如果SYSTEM_SUPPORT_OS为真，则需要支持OS.
	  OS_CPU_SR  cpu_sr;
    OS_ENTER_CRITICAL();  //保存全局中断标志,关总中断/* Tell uC/OS-II that we are starting an ISR*/
    OSIntNesting++;       //OSSemPost(NMEA_MBOX);
    OS_EXIT_CRITICAL();   //恢复全局中断标志   
#endif
	if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)  
		{
		Res =USART_ReceiveData(USART2);	//读取接收到的数据
		
		if((usart2.RX_STA&0x8000)==0)//接收未完成
			{
			if(usart2.RX_STA&0x4000)//接收到了0x0d
				{
					if(Res!=0x0a)
						usart2.RX_STA=0;//接收错误,重新开始
					else 
						usart2.RX_STA|=0x8000;	//接收完成了 
				}
			else //还没收到0X0D
				{	
				if(Res==0x0d)
					usart2.RX_STA|=0x4000;
				else
					{
					usart2.RX_Buf[usart2.RX_STA&0X3FFF]=Res ;
					usart2.RX_STA++;
					if(usart2.RX_STA>(58-1))
						usart2.RX_STA=0;//接收数据错误,重新开始接收	
					drv_uart_tx_bytes( &Res, 1 );		//直接发送回去 没有哦逻辑			
					}		 
				}
			}   		 
     } 
#if SYSTEM_SUPPORT_OS 	//如果SYSTEM_SUPPORT_OS为真，则需要支持OS.
	 OSIntExit();											 
#endif
} 


